#1 SpringBoot-cache
SpringbootCacheApplication 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;@MapperScan ("com.atguigu.cache.mapper" )@SpringBootApplication @EnableCaching public class Springboot01CacheApplication { public static void main (String[] args) { SpringApplication.run(Springboot01CacheApplication.class, args); } }
Bean–Department 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package com.atguigu.cache.bean;public class Department { private Integer id; private String departmentName; public Department () { super (); } public Department (Integer id, String departmentName) { super (); this .id = id; this .departmentName = departmentName; } public Integer getId () { return id; } public void setId (Integer id) { this .id = id; } public String getDepartmentName () { return departmentName; } public void setDepartmentName (String departmentName) { this .departmentName = departmentName; } @Override public String toString () { return "Department [id=" + id + ", departmentName=" + departmentName + "]" ; } }
Bean—Employee 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 package com.atguigu.cache.bean;import java.io.Serializable;public class Employee implements Serializable { private Integer id; private String lastName; private String email; private Integer gender; private Integer dId; public Employee () { super (); } public Employee (Integer id, String lastName, String email, Integer gender, Integer dId) { super (); this .id = id; this .lastName = lastName; this .email = email; this .gender = gender; this .dId = dId; } public Integer getId () { return id; } public void setId (Integer id) { this .id = id; } public String getLastName () { return lastName; } public void setLastName (String lastName) { this .lastName = lastName; } public String getEmail () { return email; } public void setEmail (String email) { this .email = email; } public Integer getGender () { return gender; } public void setGender (Integer gender) { this .gender = gender; } public Integer getdId () { return dId; } public void setdId (Integer dId) { this .dId = dId; } @Override public String toString () { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", dId=" + dId + "]" ; } }
config—myRedisConfig 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 package com.atguigu.cache.config;import com.atguigu.cache.bean.Department;import com.atguigu.cache.bean.Employee;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import java.net.UnknownHostException;@Configuration public class MyRedisConfig { @Bean public RedisTemplate<Object, Employee> empRedisTemplate ( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Employee> template = new RedisTemplate<Object, Employee>(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer<Employee> ser = new Jackson2JsonRedisSerializer<Employee>(Employee.class); template.setDefaultSerializer(ser); return template; } @Bean public RedisTemplate<Object, Department> deptRedisTemplate ( RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { RedisTemplate<Object, Department> template = new RedisTemplate<Object, Department>(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer<Department> ser = new Jackson2JsonRedisSerializer<Department>(Department.class); template.setDefaultSerializer(ser); return template; } @Primary @Bean public RedisCacheManager employeeCacheManager (RedisTemplate<Object, Employee> empRedisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(empRedisTemplate); cacheManager.setUsePrefix(true ); return cacheManager; } @Bean public RedisCacheManager deptCacheManager (RedisTemplate<Object, Department> deptRedisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(deptRedisTemplate); cacheManager.setUsePrefix(true ); return cacheManager; } }
config—mycacheconfig 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com.atguigu.cache.config; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.lang.reflect.Method; import java.util.Arrays; @Configuration public class MyCacheConfig { @Bean("myKeyGenerator") public KeyGenerator keyGenerator(){ return new KeyGenerator(){ @Override public Object generate(Object target, Method method, Object... params) { return method.getName()+"["+ Arrays.asList(params).toString()+"]"; } }; } }
DeptService 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 package com.atguigu.cache.service;import com.atguigu.cache.bean.Department;import com.atguigu.cache.mapper.DepartmentMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.cache.Cache;import org.springframework.cache.annotation.CacheConfig;import org.springframework.cache.annotation.Cacheable;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.stereotype.Service;@Service public class DeptService { @Autowired DepartmentMapper departmentMapper; @Qualifier ("deptCacheManager" ) @Autowired RedisCacheManager deptCacheManager; public Department getDeptById (Integer id) { System.out.println("查询部门" +id); Department department = departmentMapper.getDeptById(id); Cache dept = deptCacheManager.getCache("dept" ); dept.put("dept:1" ,department); return department; } }
EmployeeService 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 package com.atguigu.cache.service;import com.atguigu.cache.bean.Employee;import com.atguigu.cache.mapper.EmployeeMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.*;import org.springframework.stereotype.Service;@CacheConfig (cacheNames="emp" ) @Service public class EmployeeService { @Autowired EmployeeMapper employeeMapper; @Cacheable (value = {"emp" }) public Employee getEmp (Integer id) { System.out.println("查询" +id+"号员工" ); Employee emp = employeeMapper.getEmpById(id); return emp; } @CachePut (key = "#result.id" ) public Employee updateEmp (Employee employee) { System.out.println("updateEmp:" +employee); employeeMapper.updateEmp(employee); return employee; } @CacheEvict (value="emp" ,beforeInvocation = true ) public void deleteEmp (Integer id) { System.out.println("deleteEmp:" +id); int i = 10 /0 ; } @Caching ( cacheable = { @Cacheable (key = "#lastName" ) }, put = { @CachePut (key = "#result.id" ), @CachePut (key = "#result.email" ) } ) public Employee getEmpByLastName (String lastName) { return employeeMapper.getEmpByLastName(lastName); } }
2 SpringBoot -amqp SpringBootAmqpApplication 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.atguigu.amqp;import org.springframework.amqp.rabbit.annotation.EnableRabbit;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableRabbit @SpringBootApplication public class Springboot02AmqpApplication { public static void main (String[] args) { SpringApplication.run(Springboot02AmqpApplication.class, args); } }
BookService 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.atguigu.amqp.service;import com.atguigu.amqp.bean.Book;import org.springframework.amqp.core.Message;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Service;@Service public class BookService { @RabbitListener (queues = "atguigu.news" ) public void receive (Book book) { System.out.println("收到消息:" +book); } @RabbitListener (queues = "atguigu" ) public void receive02 (Message message) { System.out.println(message.getBody()); System.out.println(message.getMessageProperties()); } }
MyAMQPConfig 1 2 3 4 5 6 7 8 @Configuration public class MyAMQPConfig { @Bean public MessageConverter messageConverter(){ return new Jackson2JsonMessageConverter(); } }
3 SpringBoot-elastic 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.atguigu.elastic;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class Springboot03ElasticApplication { public static void main (String[] args) { SpringApplication.run(Springboot03ElasticApplication.class, args); } }
BookRepository 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.atguigu.elastic.repository;import com.atguigu.elastic.bean.Book;import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;import java.util.List;public interface BookRepository extends ElasticsearchRepository <Book ,Integer > { public List<Book> findByBookNameLike (String bookName) ; }
Book 1 2 3 4 5 6 7 8 9 @Document (indexName = "atguigu" ,type = "book" )public class Book { private Integer id; private String bookName; private String author; public Integer getId () { return id; }
properties 1 2 3 4 spring.elasticsearch.jest.uris=http: spring.data.elasticsearch.cluster-name=elasticsearch spring.data.elasticsearch.cluster-nodes=118.24 .44.169:9301
4 SpringBoot -task SpringBoot-TaskApplication 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.atguigu.task;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.scheduling.annotation.EnableScheduling;@EnableAsync @EnableScheduling @SpringBootApplication public class Springboot04TaskApplication { public static void main (String[] args) { SpringApplication.run(Springboot04TaskApplication.class, args); } }
AsyncService 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.atguigu.task.service;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;@Service public class AsyncService { @Async public void hello () { try { Thread.sleep(3000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("处理数据中..." ); } }
ScheduleService 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com.atguigu.task.service;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;@Service public class ScheduledService { @Scheduled (cron = "0/4 * * * * MON-SAT" ) public void hello () { System.out.println("hello ... " ); } }
Asyncontroller 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.atguigu.task.controller;import com.atguigu.task.service.AsyncService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestController public class AsyncController { @Autowired AsyncService asyncService; @GetMapping ("/hello" ) public String hello () { asyncService.hello(); return "success" ; } }
properties 1 2 3 4 spring.mail.username=534096094@qq.com spring.mail.password=gtstkoszjelabijb spring.mail.host=smtp.qq.com spring.mail.properties.mail.smtp.ssl.enable=true
5 Spring-security SpringBootSecurityApplication 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 package com.atguigu.security;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class Springboot05SecurityApplication { public static void main (String[] args) { SpringApplication.run(Springboot05SecurityApplication.class, args); } }
MySecurityConfig 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 package com.atguigu.security.config;import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@EnableWebSecurity public class MySecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure (HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/" ).permitAll() .antMatchers("/level1/**" ).hasRole("VIP1" ) .antMatchers("/level2/**" ).hasRole("VIP2" ) .antMatchers("/level3/**" ).hasRole("VIP3" ); http.formLogin().usernameParameter("user" ).passwordParameter("pwd" ) .loginPage("/userlogin" ); http.logout().logoutSuccessUrl("/" ); http.rememberMe().rememberMeParameter("remeber" ); } @Override protected void configure (AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("zhangsan" ).password("123456" ).roles("VIP1" ,"VIP2" ) .and() .withUser("lisi" ).password("123456" ).roles("VIP2" ,"VIP3" ) .and() .withUser("wangwu" ).password("123456" ).roles("VIP1" ,"VIP3" ); } }
Controller 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 package com.atguigu.security.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;@Controller public class KungfuController { private final String PREFIX = "pages/" ; @GetMapping ("/" ) public String index () { return "welcome" ; } @GetMapping ("/userlogin" ) public String loginPage () { return PREFIX+"login" ; } @GetMapping ("/level1/{path}" ) public String level1 (@PathVariable("path" ) String path) { return PREFIX+"level1/" +path; } @GetMapping ("/level2/{path}" ) public String level2 (@PathVariable("path" ) String path) { return PREFIX+"level2/" +path; } @GetMapping ("/level3/{path}" ) public String level3 (@PathVariable("path" ) String path) { return PREFIX+"level3/" +path; } }
welcome 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 <!DOCTYPE html> <html xmlns:th ="http://www.thymeleaf.org" xmlns:sec ="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4" > <head > <meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" > <title > Insert title here</title > </head > <body > <h1 align ="center" > 欢迎光临武林秘籍管理系统</h1 > <div sec:authorize ="!isAuthenticated()" > <h2 align ="center" > 游客您好,如果想查看武林秘籍 <a th:href ="@{/userlogin}" > 请登录</a > </h2 > </div > <div sec:authorize ="isAuthenticated()" > <h2 > <span sec:authentication ="name" > </span > ,您好,您的角色有: <span sec:authentication ="principal.authorities" > </span > </h2 > <form th:action ="@{/logout}" method ="post" > <input type ="submit" value ="注销" /> </form > </div > <hr > <div sec:authorize ="hasRole('VIP1')" > <h3 > 普通武功秘籍</h3 > <ul > <li > <a th:href ="@{/level1/1}" > 罗汉拳</a > </li > <li > <a th:href ="@{/level1/2}" > 武当长拳</a > </li > <li > <a th:href ="@{/level1/3}" > 全真剑法</a > </li > </ul > </div > <div sec:authorize ="hasRole('VIP2')" > <h3 > 高级武功秘籍</h3 > <ul > <li > <a th:href ="@{/level2/1}" > 太极拳</a > </li > <li > <a th:href ="@{/level2/2}" > 七伤拳</a > </li > <li > <a th:href ="@{/level2/3}" > 梯云纵</a > </li > </ul > </div > <div sec:authorize ="hasRole('VIP3')" > <h3 > 绝世武功秘籍</h3 > <ul > <li > <a th:href ="@{/level3/1}" > 葵花宝典</a > </li > <li > <a th:href ="@{/level3/2}" > 龟派气功</a > </li > <li > <a th:href ="@{/level3/3}" > 独孤九剑</a > </li > </ul > </div > </body > </html >
loginhtml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1 align="center">欢迎登陆武林秘籍管理系统</h1> <hr> <div align="center"> <form th:action="@{/userlogin}" method="post"> 用户名:<input name="user"/><br> 密码:<input name="pwd"><br/> <input type="checkbox" name="remeber"> 记住我<br/> <input type="submit" value="登陆"> </form> </div> </body> </html>
6 springboot-dubbo consumer-user 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import com.alibaba.dubbo.config.annotation.Reference;import com.atguigu.ticket.service.TicketService;import org.springframework.stereotype.Service;@Service public class UserService { @Reference TicketService ticketService; public void hello () { String ticket = ticketService.getTicket(); System.out.println("买到票了:" +ticket); } }
application.properties 1 2 3 dubbo.application.name=consumer-user dubbo.registry.address=zookeeper:
1 2 3 4 5 6 7 8 @Component @Service public class TicketServiceImpl implements TicketService { @Override public String getTicket () { return "《厉害了,我的国》" ; } }
1 2 3 4 5 dubbo.application.name=provider-ticket dubbo.registry.address=zookeeper://118.24.44.169:2181 dubbo.scan.base-packages=com.atguigu.ticket.service
SpringCloud 1 2 3 4 5 6 7 8 9 10 11 12 13 /** * 注册中心 * 1、配置Eureka信息 * 2、@EnableEurekaServer */ @EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
1 2 3 4 5 6 7 8 9 10 server: port: 8761 eureka: instance: hostname: eureka-server # eureka实例的主机名 client: register-with-eureka: false #不把自己注册到eureka上 fetch-registry: false #不从eureka上来获取服务的注册信息 service-url: defaultZone: http://localhost:8761/eureka/
1 2 3 4 5 6 7 8 9 10 11 12 13 server: port: 8002 spring: application: name: provider-ticket eureka: instance: prefer-ip-address: true # 注册服务的时候使用服务的ip地址 client: service-url: defaultZone: http://localhost:8761/eureka/
Springboot Actuar 1 2 3 4 5 6 7 8 9 10 11 12 13 14 /** * 自定义健康状态指示器 * 1、编写一个指示器 实现 HealthIndicator 接口 * 2、指示器的名字 xxxxHealthIndicator * 3、加入容器中 */ @SpringBootApplication public class Springboot08ActuatorApplication { public static void main(String[] args) { SpringApplication.run(Springboot08ActuatorApplication.class, args); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com.atguigu.springboot08actuator.health; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component public class MyAppHealthIndicator implements HealthIndicator { @Override public Health health() { //自定义的检查方法 //Health.up().build()代表健康 return Health.down().withDetail("msg","服务异常").build(); } }
1 2 3 git.branch=master git.commit.id=xjkd33s git.commit.time=2017-12-12 12:12:56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 management.security.enabled=false spring.redis.host=118.24.44.169 info.app.id=hello info.app.version=1.0.0 #endpoints.metrics.enabled=false endpoints.shutdown.enabled=true #endpoints.beans.id=mybean #endpoints.beans.path=/bean #endpoints.beans.enabled=false # #endpoints.dump.path=/du # \u5173\u95ED\u6240\u6709\u7AEF\u70B9\u8BBF\u95EE #endpoints.enabled=false #endpoints.beans.enabled=true management.context-path=/manage management.port=8181
1
Author:
John Doe
Permalink:
http://yoursite.com/2018/11/11/Spring Boot/SpringBoot高级雷丰阳/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY?